package com.example.smartcity.ui.SmartCommynity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.DelivaryListViewAdapter;
import com.example.smartcity.ui.Adapter.ExpressManagementAdapter;
import com.example.smartcity.ui.Adapter.SelfDivListViewAdapter;
import com.example.smartcity.ui.Bean.DelivaryMessageBean;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ExpressManagement extends AppCompatActivity {

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ArrayList<View> list = new ArrayList<>();
    private List<String> titles = Arrays.asList("快递点详情","我的快递");
    private ArrayList<DelivaryMessageBean>  DeliveryMessageList = new ArrayList<>();
    private static ArrayList<DelivaryMessageBean>  DeliverySelfMessageList = new ArrayList<>();
    View view2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_express_management);
        tabLayout = findViewById(R.id.tabLayout);
        viewPager = findViewById(R.id.viewPager);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){

        tabLayout.setupWithViewPager(viewPager);

        View view1 = LayoutInflater.from(ExpressManagement.this).inflate(R.layout.delivaerymessagelayout,null);
        view2 = LayoutInflater.from(ExpressManagement.this).inflate(R.layout.mydelivery,null);
        list.add(view1);
        list.add(view2);
        ListView listView = view1.findViewById(R.id.listView);
        loadDeliveryMessageList();

        listView.setAdapter(new DelivaryListViewAdapter(getBaseContext(),R.layout.delivaryitemlayout,DeliveryMessageList));
        loadSelfDel();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ExpressManagement.this.getBaseContext(),GetCode.class);
                intent.putExtra("Message",DeliveryMessageList.get(position));
                startActivityForResult(intent,1);
            }
        });
        viewPager.setAdapter(new ExpressManagementAdapter(list,titles));
    }

    //接收返回数据

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1:
                if (resultCode==RESULT_OK){
                    DelivaryMessageBean bean = (DelivaryMessageBean) data.getSerializableExtra("resMes");
                    DeliverySelfMessageList.add(bean);
                    viewPager.setCurrentItem(1);
                    loadSelfDel();
                }
                break;
        }
    }
    //加载我的快递
    public void loadSelfDel(){
        ListView LV_SelfDel = view2.findViewById(R.id.LV_selfDel);
        LV_SelfDel.setAdapter(new SelfDivListViewAdapter(getBaseContext(),R.layout.selfdelitemlayout,DeliverySelfMessageList));
    }


    //加载快递点详情数据
    public void loadDeliveryMessageList(){
        DelivaryMessageBean bean = new DelivaryMessageBean("海德北岸17栋晨光妈妈驿站","海德北岸城17栋601","02586169828");
        DelivaryMessageBean bean1 = new DelivaryMessageBean("海德北岸14栋小牛驿站","海德北岸城14栋101","02586169828");
        DelivaryMessageBean bean2 = new DelivaryMessageBean("海德北岸20栋中通驿站","海德北岸城20栋101","02586169828");
        DelivaryMessageBean bean3 = new DelivaryMessageBean("海德北岸21栋京东驿站","海德北岸城21栋102","02586169828");
        DeliveryMessageList.add(bean);
        DeliveryMessageList.add(bean1);
        DeliveryMessageList.add(bean2);
        DeliveryMessageList.add(bean3);

    }
}